package com.yunmang.activity.statictis;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.yunmang.R;
import com.yunmang.activity.BaseActivity;
import com.yunmang.activity.SearchEndActivity;
import com.yunmang.adapter.StaticticsSavingAdapter;
import com.yunmang.app.AppConstant;
import com.yunmang.entity.SavingStatictics;
import com.yunmang.entity.event.EndEvent;
import com.yunmang.http.HttpUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;
import rx.Subscriber;

/**
 * Created by Administrator on 2017/5/22.
 */
public class SavingQueryStatictics extends BaseActivity {
    @InjectView(R.id.toolbar)
    Toolbar mToolbar;
    @InjectView(R.id.tv_check)
    TextView tvCheck;
    @InjectView(R.id.listview)
    ListView listview;
    private CustomDialog customDialog;
    private TextView btn_ok, btn_cancel;
    private Subscriber<SavingStatictics> subscriber;
    private StaticticsSavingAdapter adapter;
    private List<SavingStatictics.ModelBean> dataList;
    private TextView tv_start;
    private RadioButton rb1,rb2,rb3;
    private int EndDepartId;
    @Override
    public int getLayoutId() {
        return R.layout.activity_statictics_saving;
    }

    @Override
    public void initView() {
        ButterKnife.inject(this);
        EventBus.getDefault().register(this);
        initToolbar();
    }

    @OnClick({R.id.tv_check})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_check:
                customDialog = new CustomDialog(this, R.style.Prompt_dialog);
                if (!customDialog.isShowing()) {
                    customDialog.show();
                }
                break;
        }
    }

    private void initToolbar() {
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("库存统计");

        mToolbar.setNavigationIcon(R.mipmap.ic_back);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        new MenuInflater(this).inflate(R.menu.menu_one_text, menu);
        menu.getItem(0).setTitle("搜索");
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
            case R.id.itemOne:
                customDialog = new CustomDialog(this, R.style.Prompt_dialog);
                if (!customDialog.isShowing()) {
                    customDialog.show();
                }
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void initData() {
        dataList=new ArrayList<>();
        adapter=new StaticticsSavingAdapter(context,dataList);
        listview.setAdapter(adapter);
    }


    public class CustomDialog extends Dialog implements View.OnClickListener {

        public CustomDialog(Context context) {
            super(context);
        }

        public CustomDialog(Context context, int theme) {
            super(context, theme);
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.dialog_savingquery);
            btn_cancel = (TextView) findViewById(R.id.btn_cancel);
            tv_start = (TextView) findViewById(R.id.tv_start);
            btn_ok = (TextView) findViewById(R.id.btn_ok);
            rb1 = (RadioButton) findViewById(R.id.rb_1);
            rb2 = (RadioButton) findViewById(R.id.rb_2);
            rb3 = (RadioButton) findViewById(R.id.rb_3);
            tv_start.setOnClickListener(this);
            btn_ok.setOnClickListener(this);
            btn_cancel.setOnClickListener(this);
            this.setCancelable(true);
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.tv_start:
                    startActivity(new Intent(context, SearchEndActivity.class));
                    break;
                case R.id.btn_ok:
                    int type;
                    if(rb1.isChecked()){
                        type=0;
                    }else if(rb2.isChecked()){
                        type=2;
                    }else{
                        type=3;
                    }
                    getSavingInfo(type);
                    break;
                case R.id.btn_cancel:
                    dismiss();
                    break;
                default:
                    break;
            }
        }
    }

    private void getSavingInfo(int type) {
        showWaitingDialog("下载数据中……");
        subscriber=new Subscriber<SavingStatictics>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                customDialog.dismiss();
                e.printStackTrace();
                hideWaitingDialog();
            }

            @Override
            public void onNext(SavingStatictics savingStatictics) {
                customDialog.dismiss();
                hideWaitingDialog();
                if(savingStatictics.getCode()==200){
                    List<SavingStatictics.ModelBean> model = savingStatictics.getModel();
                    if(model.size()>0){
                        dataList=savingStatictics.getModel();
                        adapter.refreshData(dataList);
                    }else{
                        showToast("无数据");
                    }
                }else{
                    showToast("获取失败");
                }
            }
        };
        HttpUtils.getInstance().getSavingStatics(subscriber, AppConstant.userid,EndDepartId,type);
    }
    public void onEventMainThread(EndEvent event) {
        EndDepartId = event.getId();
        tv_start.setText(event.getName());
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
